<?php
namespace plugins\appmarket\controller; 
use cmf\controller\PluginAdminBaseController;
use think\Db;
use think\Validate;
use app\common\lib\AccessAuthorization;
use plugins\appmarket\controller\ModuleCategoryController;
use plugins\appmarket\lib\Upload;
use plugins\appmarket\controller\ConfigController;

/**
 * 模块菜单管理
 */
class ModuleMenuManageController extends PluginAdminBaseController
{

	private $menuDisplay 	=		[ '隐藏' , '显示' ];
	private $menuView		=		[ '只作为菜单' , '有界面可访问' , '无界面可访问' ];
	private $indent			=		30;
	private $module			=		0;
	private $moduleVersion 	=		'';
	private $mainMenuId		= 		0;
	/**
     * 模块菜单管理
     * @adminMenu(
     *     'name'   => '菜单管理',
     *     'parent' => 'AdminIndex/index',
     *     'display'=> true,
     *     'hasView'=> true,
     *     'order'  => 1000,
     *     'icon'   => '',
     *     'remark' => '菜单管理',
     *     'param'  => ''
     * )
     */
	public function index()
	{
		$param = $this->request->param();
		$symbol = cmf_parse_name ( $param ['symbol'] , 1 );
		$this->moduleId = $param [ 'moduleId' ];
		$this->moduleVersion =	$param [ 'moduleVersion' ] ;
		$where = [
			'app'				=> 		"plugin/$symbol",
			'controller'		=>		"AdminIndex",
			'action'			=>		"index"
		];
		$mainMenu  = Db::name( 'admin_menu' )->where( $where )->find();
		$mainMenuId = $mainMenu [ 'id' ];

		$this->mainMenuId = $mainMenuId;
		$mainMenu [ 'level' ] = 0;
		if( $subList = $this->getSubMenu ( $mainMenuId ) ){
			$mainMenu [ 'children' ] = $subList;
		}

		$menuList = [ $mainMenu ];
		//dump($menuList);exit;
		$data = $this->getTableStr( $menuList );
		$this->assign( 'tableStr' , $data['tableStr'] );
		$this->assign( 'editStr' , $data['editStr'] );	
		return $this->fetch( 'menu/index' );
	}

	/**
	 * 	构建树形
	 */
	private function getTableStr( $list , $parent = [] )
	{
		$data = [
			'tableStr' => '',
			'editStr' => ''
		];
		foreach ( $list as $key => $value ) {
			$is = $parent;
			$is [] = isset( $list [ $key+1 ] );
			$level  = $value [ 'level' ];
			$data['tableStr'] .= "<tr>";
			$data['editStr'] .= "<tr>";
				$indent = $this->indent * $level ;
				$icon = $this->getnbsp( $level , $is , isset( $list[ $key + 1 ] ) );
				$data['tableStr'] .= '<td>'.$value['id'].'</td>';
				$data['tableStr'] .= '<td>'.$icon.$value['name'].isset( $value[ $key + 1 ] ).'</td>';
				$data['tableStr'] .= '<td>'.$value['list_order'].'</td>';
				$data['tableStr'] .= '<td>'.$value['app'].'</td>';
				$data['tableStr'] .= '<td>'.$value['controller'].'</td>';
				$data['tableStr'] .= '<td>'.$value['action'].'</td>';
				$data['tableStr'] .= '<td>'.$this->menuView [ $value [ 'type' ] ] .'</td>';
				$data['tableStr'] .= '<td>'.$this->menuDisplay [ $value [ 'status' ] ] .'</td>';
				$data['tableStr'] .= '<td>'.$value['module_version'].'</td>';

				$moduleVersion = '*'.$this->moduleVersion.'*';
				$pos = strpos( $value [ 'module_version' ] , $moduleVersion );
				$replace = str_replace( $moduleVersion , '' , $value[ 'module_version' ] );
				$canOperate = true;
				if( $value[ 'id' ] == $this->mainMenuId || false === $pos || !empty( $replace ) ){
					$canOperate = false;
				}


				$data['editStr'] .= '<td>'.$value['id'].'</td>';
				if( $canOperate ){
					$data['editStr'] .= '<td class="x-name" >'.$icon.'<input name="name" style="display:inline-block;width:100px;" type="text" class="form-control" value="'.$value['name'].'" /></td>';
					$data['editStr'] .= '<td>'.'<input type="text" name="list_order" class="form-control" value="'.$value['list_order'].'" /></td>';
					$data['editStr'] .= '<td>'.'<input type="text" name="app" class="form-control" value="'.$value['app'].'" /></td>';
					$data['editStr'] .= '<td>'.'<input type="text" name="controller" class="form-control" value="'.$value['controller'].'" /></td>';
					$data['editStr'] .= '<td>'.'<input type="text" name="action" class="form-control" value="'.$value['action'].'" /></td>';
					
					$viewSelect = '<select name="type" class="form-control">';
					foreach ($this->menuView as $dk => $dv) {
						$selected = ( $dk == $value[ 'type' ] ) ? 'selected' : '';
						$viewSelect .= '<option value="'.$dk.'" '.$selected.'>'.$dv.'</option>';
					}
					$viewSelect .= '</select>';
					$data['editStr'] .= '<td>'.$viewSelect.'</td>';

					$displaySelect = '<select name="status" class="form-control">';
					foreach ($this->menuDisplay as $dk => $dv) {
						$selected = ( $dk == $value[ 'status' ] ) ? 'selected' : '';
						$displaySelect .= '<option value="'.$dk.'" '.$selected.' >'.$dv.'</option>';
					}
					$displaySelect .= '</select>';
					$data['editStr'] .= '<td>'.$displaySelect.'</td>';

				}else{
					$data['editStr'] .= '<td>'.$icon.$value['name'].'</td>';
					$data['editStr'] .= '<td>'.$value['list_order'].'</td>';
					$data['editStr'] .= '<td>'.$value['app'].'</td>';
					$data['editStr'] .= '<td>'.$value['controller'].'</td>';
					$data['editStr'] .= '<td>'.$value['action'].'</td>';
					$data['editStr'] .= '<td>'.$this->menuView [ $value [ 'type' ] ] .'</td>';
					$data['editStr'] .= '<td>'.$this->menuDisplay [ $value [ 'status' ] ] .'</td>';
				}
				$data['editStr'] .= '<td>'.$value['module_version'].'</td>';
				
			$data['tableStr'] .= "</tr>";
			$data['editStr'] .= "</tr>";

			//加载子级
			if( isset( $value [ 'children' ] ) && !empty( $value [ 'children' ] ) ){
				$tmp = $this->getTableStr( $value [ 'children' ] , $is );
				$data['tableStr'] .=  $tmp [ 'tableStr' ];
				$data['editStr'] .= $tmp [ 'editStr' ];
			}
		}
		return $data;
	}

	/**
	 * nbsp
	 */
	private function getnbsp( $level , $parent , $isExistsChildren  )
	{
		
		$icon = [ '│' , '├─' , '└─'];
		$nbsp = "&nbsp;&nbsp;&nbsp;";
		$str = "";

		//$level 判断循环次数   $isExistsChildren 判断符号
		for( $i = 0 ; $i <= $level - 1 ; $i ++ ){
			$str .= $nbsp;
			if( $i < $level-1 && $parent[$i+1] ){
				$str .= $icon[0];
			}
			if( $i == $level-1){
				$str .= ( $isExistsChildren ) ? $icon[1] : $icon[2] ;
			}
		}
		return $str;
	}

	/**
	 * 	根据主菜单获取子菜单id
	 */
	private function getSubMenu( $mainMenuId , $level = 1, $search = [] )
	{
		$subMenu = [];
		$list = Db::name( 'admin_menu' )->where( 'parent_id' , $mainMenuId )->select();
		foreach ( $list as $key => $value ) {
			$value [ 'level' ] = $level;
			$tmp = $this->getSubMenu( $value[ 'id' ] , $level+1 , $search );
			if( !empty( $tmp ) ){
				$value [ 'children' ] = $tmp; 
			}

			$subMenu [] = $value;
		}
		return $subMenu;
	}

	/**
	 * 更新菜单数据
	 */
	public function update()
	{	
		$param = $this->request->param();
		$update = $param[ 'update' ];
		foreach ($update as $key => $value) {
			$up = $value;
			$id = $up[ 'id' ];
			unset( $up [ 'id' ] );
			Db::name( 'admin_menu' )->where( 'id' , $id )->update( $up );
		}
		return zy_json_echo( true , '修改成功!');
	}

}